/**
 * 
 */
package com.designpattern.design.template;

/**
 * 模板方法模式-制作豆浆
 * @author zhangyang
 *
 */
public class TemplateTest {
public static void main(String[] args) {
	SoyaMilk blackBeanSoya = new BlackBeanSoya(); 
	blackBeanSoya.make();
	SoyaMilk peanutSoya = new PeanutSoya();
	peanutSoya.make();
	SoyaMilk soya = new Soya();
	soya.make();
}
}

/**
 * 抽象类 豆浆
 * @author zhangyang
 *
 */
abstract class SoyaMilk{
	
	String resource;
	String box;
	/**
	 * 添加原料-抽象方法
	 */
	public  abstract void addResource();
	
	public void soak() {
		System.out.println("原料浸泡");
	}
	public void beat() {
		System.out.println("原料打碎");
	}
	public  void boil() {
		System.out.println("将豆浆烧开");
	}
	
	/**
	 * 将豆浆装盒子里
	 */
	public abstract void putBox();
	
	/**
	 * 都将制作方法-可以做成final不允许子类去覆盖
	 */
	public final void make() {
		if(hook())
		addResource();
		soak();
		beat();
		boil();
		putBox();
		System.out.println("都将制作完成，原料是"+resource+"包装是"+box);
	}
	
	/**
	 * 钩子方法-用来确认是否添加原料
	 * 子类在实现时，钩子方法必须重写。钩子方法限定的方法空实现
	 * @return
	 */
	public Boolean hook() {
		return true;
	}
}

class PeanutSoya extends SoyaMilk{


	@Override
	public void addResource() {
		// TODO Auto-generated method stub
		this.resource="花生";
		System.out.println("花生豆浆原料装填");
	}

	@Override
	public void putBox() {
		// TODO Auto-generated method stub
		this.box="纸袋";
		System.out.println("花生豆浆包装");
	}
	
}

class BlackBeanSoya extends SoyaMilk{
	@Override
	public void addResource() {
		// TODO Auto-generated method stub
		this.resource="黑豆";
		System.out.println("黑豆豆浆原料装填");
	}

	@Override
	public void putBox() {
		// TODO Auto-generated method stub
		this.box="塑料";
		System.out.println("黑豆豆浆包装");
	}
}

class Soya extends SoyaMilk{

	@Override
	public void addResource() {
		// TODO Auto-generated method stub
		//空实现
	}

	/* (non-Javadoc)
	 * @see com.design.template.SoyaMilk#putBox()
	 */
	@Override
	public void putBox() {
		// TODO Auto-generated method stub
		this.box="牛奶包";
		System.out.println("存豆浆包装");
	}

	@Override
	public Boolean hook() {
		// TODO Auto-generated method stub
		return false;
	}
	
	
}